﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace OnlineManagement.Web.userControl
{
    public partial class UCPigPenSettings :BaseUserControl
    {
        protected int _hoggeryId = 0;
        private string strPath = "";
        protected void Page_Load(object sender, EventArgs e)
        {
           // linkUpdate.ServerClick += new EventHandler(linkUpdate_ServerClick);
            _hoggeryId = HoggeryID;
            if(_hoggeryId<=0){
                panEmpty.Visible = true;
                PanPigpenAdd.Visible = false;
                panShow.Visible = false;
                return;
            }
            strPath = this.Page.Request.ServerVariables["PATH_INFO"];
            rptShow.ItemDataBound += new RepeaterItemEventHandler(rptShow_ItemDataBound);
            btnSave.Click+=new EventHandler(btnSave_Click);
            if (!Page.IsPostBack)
            {
                txtHoggeryName.Text = HoggeryName;
                BindData(_hoggeryId);
            }
        }

       

        void rptShow_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || 
                e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Label lbFeederName = e.Item.FindControl("lbFeederName") as Label;
                Label lbOperation = e.Item.FindControl("lbOperation") as Label;

                DataRowView dv = (DataRowView)e.Item.DataItem;
                string feederName = dv["FeederName"].ToString();
                string pigPenId = dv["PigPenId"].ToString();
                lbFeederName.Text = string.IsNullOrEmpty(feederName) ? Common.PubConstant.EmptyString : feederName;

                string countStr = dv["CurrentPigCount"].ToString();
                //System.Text.StringBuilder sb = new System.Text.StringBuilder();

                if (!string.IsNullOrEmpty(countStr) && countStr == "0")
                {

                    //sb.AppendFormat("<a href=\"../webs/PigPenDelete.aspx?pigPenId={0}&customizeID={1}\" id=\"linkDelete\" title=\"删除\" rel=\"modal\" > <img src=\"../resources/images/icons/cross.png\" alt=\"删除\" /></a>", pigPenId, countStr);
                    lbOperation.Text = "<a href=\"#panDelete\" id=\"linkDelete\" title=\"删除\" onclick=\"deletePigPen(this);\" > <img src=\"../resources/images/icons/cross.png\" alt=\"删除\" /></a>";
                }
                else {
                    lbOperation.Text = "<a href=\"javascript:void(0);\" onclick=\"undelete();\" title=\"删除\" > <img src=\"../resources/images/icons/cross.png\" alt=\"删除\" /></a>";
                }
            }

        }

        private void BindData(int hoggeryId) {
            int rCount;
            rptShow.PageSize = 5;
            rptShow.PageLink = strPath;
            rptShow.CurrentPage = Request.QueryString["page"] == null ? 1 : int.Parse(Request.QueryString["page"] == "" ? "1" : Request.QueryString["page"]);

            try
            {
                BLL.Dal.PigPen bllPP = new BLL.Dal.PigPen();

                DataSet ds = bllPP.PigPensByPaging(hoggeryId, string.Empty, string.Empty, rptShow.CurrentPage, rptShow.PageSize, out rCount);
                if (ds != null && ds.Tables.Count > 1)
                {
                    rptShow.DataSource = ds.Tables[1];
                    rptShow.RecordCount = rCount;
                    rptShow.DataBind();
                }
            }catch(Exception ex){
                
            }
        }

       
        #region button event


        void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (string.IsNullOrEmpty(txtDong.Text) || string.IsNullOrEmpty(txtLan.Text))
                {
                    Common.MessageBox.PrintMsg(panMsg, Common.MsgLevelEnum.attention, "猪舍栋号或栏号不能为空，请重新输入。");
                    return;
                }
                if(HoggeryID==0||string.IsNullOrEmpty(txtHoggeryName.Text)){
                    Common.MessageBox.PrintMsg(panMsg, Common.MsgLevelEnum.attention, Common.PubMsg.NoOnlineMgrData);
                    return;
                }
               
                string feederName = txtFeeder.Text.Trim();
                string pigpenCustomerID = string.Format("{0}栋{1}号", txtDong.Text.Trim(), txtLan.Text.Trim());
                string description = txtComment.Text;
                Model.PigPen pp = new Model.PigPen() { HoggeryID = _hoggeryId, CustomizeID = pigpenCustomerID,CurrentPigCount=0, PigPenDescription = description };


                BLL.Dal.PigPen bllPen = new BLL.Dal.PigPen();
                string returnValue = string.Empty;
                bool result = bllPen.Add(pp, feederName, out returnValue);
                if (result)
                {
                    Common.MessageBox.PrintMsg(panMsg, Common.MsgLevelEnum.success, returnValue);
                }
                else
                {
                    Common.MessageBox.PrintMsg(panMsg, Common.MsgLevelEnum.error, returnValue);
                }
            }catch(Exception ex){
                Common.MessageBox.PrintMsg(panMsg, Common.MsgLevelEnum.error, ex.Message);
            }
            txtLan.Text = string.Empty;
        }

        #endregion

    }
}